# coding=utf-8
import requests
from work_file.a001 import Sm2_sign
from common import paixu
import time
import json

def send_msg(app_id, method, sm2_private_key, public_key):
    url = "http://172.16.22.77:8080"
    timeArray = time.localtime(time.time())
    data_time = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    print(data_time)
    biz_content = {
        "msgTypeCode":"414test001",
        "clientId":"2021041600241",
        "msgChannelId":"1",
        "nsrmc":"小雷科技",
        "djxh":"134235342513124",
        "title":"来了来了，高速队列的消息来了3.23 ---------333333",
        "content":"来了来了，高速队列的消息来了3.23 ---------333333",
        "acceptType":"1,2,3,6"
    }
    data = {
        "app_id":app_id,
        "method":method,
        "format":"json",
        "charset":'utf-8',
        "timestamp":data_time,
        "version": "1.0",
        "biz_content": str(biz_content)
    }
    par_data = paixu.paixu(data)
        # print(par_data)
    # time_str_a = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    # par_data = '''app_id=2021041600239&biz_content={"msgTypeCode":"0101","clientId":"2021041600239","msgChannelId":"1",
    # "nsrmc":"小雷科技","djxh":"134235342513124","title":"来了来了，高速队列的消息来了3.23 ---------333333","content":"来了来了，
    # 高速队列的消息来了3.23 ---------333333","acceptType":"1,2,3,6"}
    # &charset=utf-8&format=json&method=sendMsg.single&timestamp=%s&version=1.0''' %time_str_a
    # byte_data = [x for x in bytearray(par_data.encode())]
    byte_data = bytearray(par_data, 'utf-8')
    print(byte_data)
    sm2 = Sm2_sign(sm2_private_key, public_key)
    sign = sm2.sign(byte_data)
    vri = sm2.verify_sign(sign, byte_data)
    print(vri)
    data['sign'] = sign
    result = requests.post(url, json=data).json()
    print(result)




if __name__ == "__main__":
    app_id = "2021042100245"
    method = "sendMsg.single"
    SM2_PUBLIC_KEY = "44e7491166bf4f84f59c0aa96333e04d8cd7a533e19362b56bc4baf2cf5ea1fbafed7f277fb175cb3d8610c2015f89a63ed2b2056d631e8e3e3b9ccb6a07a4dd"
    private_key = "6f3350d20113a4c35c17afb49ae2677c3d26e7c383b45edddae507c221676d3f"
    send_msg(app_id,method, private_key, SM2_PUBLIC_KEY)